# redis中的操作-配置redis
import redis
import json

with open('../config.py', 'r', encoding="utf-8") as f:
    exec(f.read())

redisinfo0 = redisinfo0
redisinfo1 = redisinfo1
redisinfo2 = redisinfo2
redisinfo3 = redisinfo3
redisinfo4 = redisinfo4
redisinfo5 = redisinfo5
redisinfo6 = redisinfo6
redisinfo7 = redisinfo7
redisinfo8 = redisinfo8
redisinfo9 = redisinfo9
redisinfo10 = redisinfo10
redisinfo11 = redisinfo11
redisinfo12 = redisinfo12
redisinfo15 = redisinfo15
log_list = guidanginfo['logs']


class RedisArchive:
    def __init__(self):
        self.redis0 = self.get_redis(redisinfo0)  # 1vn
        self.redis1 = self.get_redis(redisinfo1)  # log
        self.redis2 = self.get_redis(redisinfo2)  # acvs_acvs
        self.redis3 = self.get_redis(redisinfo3)  # 建档的face_acvs
        self.redis4 = self.get_redis(redisinfo4)  # face_acvs
        self.redis5 = self.get_redis(redisinfo5)  # acvs_face
        self.redis6 = self.get_redis(redisinfo6)  # card_acvs
        self.redis7 = self.get_redis(redisinfo7)  # acvs_card
        self.redis8 = self.get_redis(redisinfo8)  # card_face
        self.redis9 = self.get_redis(redisinfo9)  # face_card
        self.redis10 = self.get_redis(redisinfo10)  # delete_card
        self.redis11 = self.get_redis(redisinfo11)  # del_acvs_acvs
        self.redis12 = self.get_redis(redisinfo12)  # del_face_acvs
        self.redis15 = self.get_redis(redisinfo15)  # allimg
        self.log_list = log_list
        self.ovn_key = "HIT_RESULT"
        self.log_name = "logBase"

    @staticmethod
    def get_redis(redisinfo):
        redispool = redis.ConnectionPool(host=redisinfo['ip'], port=redisinfo['port'], password=redisinfo['password'],
                                         db=redisinfo['db'], decode_responses=True)
        # redispool = redis.ConnectionPool(host="192.168.1.43", port=61001, password="12345678",
        #                                  db=redisinfo['db'], decode_responses=True)
        return redis.Redis(connection_pool=redispool, decode_responses=True)

    # --------------db0 1:n
    def rdb_queue_pop_ovn(self):
        v = self.redis0.rpop(self.ovn_key)
        if v is None:
            return None
        return v

    # --------------db1 log

    def rdb_queue_push_log_add_acvs(self, acvs_id, faceid):
        s = 'AddAcvs&' + acvs_id + '>' + faceid
        for queueLog in self.log_list:
            self.redis1.lpush(queueLog, s)

    def rdb_queue_push_log_unacvs(self, faceid):
        # 没有档案的脸
        s = 'UnAcvs&' + faceid
        for queueLog in self.log_list:
            self.redis1.lpush(queueLog, s)

    def rdb_queue_push_log_card_dict(self, face_id, card_dic):

        s = 'CardDict&' + face_id + '>' + json.dumps(card_dic)
        for queueLog in self.log_list:
            self.redis1.lpush(queueLog, s)

    def rdb_queue_push_log_ins_acvs(self, face_image_id, acvs_id, count):
        s = 'InsAcvs&' + face_image_id + '>' + acvs_id + '>' + str(count)
        for queueLog in self.log_list:
            self.redis1.lpush(queueLog, s)

    def rdb_queue_push_log_acvs_acvs(self, acvs1, acvs2, count):
        s = 'AcvsAcvs&' + acvs1 + '>' + acvs2 + '>' + str(count)
        for queueLog in self.log_list:
            self.redis1.lpush(queueLog, s)

    def rdb_queue_log_merge_acvs(self, acvs_p_id, acvs_list):
        s = 'MergeAcvs&' + acvs_p_id + '>' + json.dumps(acvs_list)
        for queueLog in self.log_list:
            self.redis1.lpush(queueLog, s)

    def rdb_queue_log_hide_card(self, acvs_id):
        s = 'HideCard&' + acvs_id
        for queueLog in self.log_list:
            self.redis1.lpush(queueLog, s)

    def rdb_queue_push_log_add_card(self, card_id, acvs_id, score_sim, faceid):
        s = 'Card&' + card_id + '>' + acvs_id + '>' + str(score_sim) + '>' + faceid
        for queueLog in self.log_list:
            self.redis1.lpush(queueLog, s)

    def rdb_queue_push_log_add_cardf(self, card_id, face_id, card_score):
        s = 'Cardf&' + card_id + '>' + face_id + '>' + str(card_score)
        for queueLog in self.log_list:
            self.redis1.lpush(queueLog, s)

    def rdb_queue_pop_log(self):
        v = self.redis1.rpop(self.log_name)
        if v is None:
            return None
        return v

    # --------------db2 acvs_acvs
    def rdb_zadd_acvs_acvs(self, acvs1_id, acvs2_id, card_score):
        return self.redis2.zadd(acvs1_id, {acvs2_id: card_score})

    def rdb_has_acvs_acvs(self, acvs1_id, acvs2_id):
        return self.redis2.zrank(acvs1_id, acvs2_id) is not None

    def rdb_add_acvs_acvs(self, acvs1_id, acvs2_id):
        return self.redis2.zincrby(name=acvs1_id, value=acvs2_id, amount=1)

    def rdb_get_acvs_acvs(self, acvs1_id, acvs2_id):
        return int(self.redis2.zscore(acvs1_id, acvs2_id))

    # --------------db3  addacvs face_new_acvs
    def rdb_add_acvs(self, face_id, acvs_id):
        return self.redis3.sadd(face_id, acvs_id)

    def rdb_get_face_new_acvs(self, face_id):
        if face_id in self.redis3.keys():
            return self.redis3.srandmember(face_id)
        return None

    # --------------db4 face-acvsall
    # 获取face对应的所有档
    def rdb_get_face_acvsall(self, facedet, face_id, level_dic, start=0, end=-1):
        # 由大到小排列
        arr = self.redis4.zrevrange(face_id, start, end, True)
        if len(arr) == 0:
            if facedet:
                if '0' in level_dic:
                    level_dic['0'].append(face_id)
                else:
                    level_dic['0'] = []
                    level_dic['0'].append(face_id)
        for a in arr:
            # 浮点数为-7 整数为-5
            level = str(a[1])[:-7]
            if level not in level_dic:
                level_dic[level] = []
                level_dic[level].append((a[0], face_id))
            else:
                level_dic[level].append((a[0], face_id))
        return level_dic

    def rdb_get_face_acvsall_null(self, face_id, level_dic, start=0, end=-1):
        # 由大到小排列
        arr = self.redis4.zrevrange(face_id, start, end, True)
        if len(arr) == 0:
            if '0' in level_dic:
                level_dic['0'].append(face_id)
            else:
                level_dic['0'] = []
                level_dic['0'].append(face_id)
        return level_dic

    def rdb_get_ovo_face_acvs(self, face_id, start=0, end=-1):
        arr = self.redis4.zrevrange(face_id, start, end, True)
        if len(arr) == 0:
            return arr

        else:
            ovo_acvs_list = []
            for a in arr:
                ovo_acvs_list.append(a[0])
            return ovo_acvs_list

    def rdb_zadd_face_acvs(self, face_id, acvs_id, score):
        return self.redis4.zadd(face_id, {acvs_id: score})

    # def RDB_addFaceacvs(face_id, acvs_id, score):
    #     return redis4.zincrby(name=face_id, value=acvs_id, amount=score)

    def rdb_has_face_acvs(self, face_id, acvs_id):
        return self.redis4.zrank(face_id, acvs_id) is not None

    def rdb_get_face_acvs(self, face_id, acvs_id):
        return self.redis4.zscore(face_id, acvs_id)

    # --------------db5 acvs_faceall
    def rdb_get_acvs_len1(self, acvs_id):
        if acvs_id is None:
            return 0
        return self.redis5.zcount(acvs_id, 100000, 200000)

    # def RDB_addAcvsFace(acvs_id, face_id, score):
    #     return redis5.zincrby(name=acvs_id, value=face_id, amount=score)
    def rdb_zadd_acvs_face(self, acvs_id, face_id, score):
        return self.redis5.zadd(acvs_id, {face_id: score})

    def rdb_get_acvs_face(self, acvs_id, face_id):
        return self.redis5.zscore(acvs_id, face_id)

    def rdb_has_acvs_face(self, acvs_id, face_id):
        return self.redis5.zrank(acvs_id, face_id) is not None

    # --------------db6 card_acvs
    def rdb_zadd_card_arch(self, card_id, acvs_id, score):
        return self.redis6.zadd(card_id, {acvs_id: score})

    def rdb_has_card_arch(self, card_id, acvs_id):
        return self.redis6.zrank(card_id, acvs_id) is not None

    # def RDB_addcardarch(card_id, acvs_id, score):
    #     return redis6.zincrby(name=card_id, value=acvs_id, amount=score)

    def rdb_get_card_score(self, card_id, acvs_id):
        return self.redis6.zscore(card_id, acvs_id)

    # --------------db7 acvs_card
    def rdb_zadd_acvs_card(self, acvs_id, card_id, score):
        return self.redis7.zadd(acvs_id, {card_id: score})

    # def RDB_hasacvscard(acvs_id, card_id):
    #     return redis7.zrank(acvs_id, card_id) != None
    # def RDB_addacvscard(acvs_id, card_id, score):
    #     return redis7.zincrby(name=acvs_id, value=card_id, amount=score)

    # --------------db8 card_face  不分级别 其他全部分级别
    def rdb_zadd_card_face(self, card_id, face_id, card_score):
        return self.redis8.zadd(card_id, {face_id: card_score})

    def rdb_has_card_face(self, card_id, face_id):
        return self.redis8.zrank(card_id, face_id) is not None

    def rdb_add_card_face(self, card_id, face_id):
        return self.redis8.zincrby(name=card_id, value=face_id, amount=0)

    # --------------db9 face_card   # 不分级别
    def rdb_zadd_face_card(self, card_id, face_id, card_score):
        return self.redis9.zadd(card_id, {face_id: card_score})

    def rdb_has_face_card(self, card_id, face_id):
        return self.redis9.zrank(card_id, face_id) is not None

    def rdb_add_face_card(self, card_id, face_id):
        return self.redis9.zincrby(name=card_id, value=face_id, amount=0)

    # --------------db10 delete_card
    def rdb_delete_card(self, card_id):
        del_list = self.redis10.smembers("delete")
        if card_id in del_list:
            return False
        return True

    # --------------db11
    def rdb_acvs_acvs(self, acvs_id):
        del_list = list(self.redis11.smembers(acvs_id))
        return del_list

    # --------------db12
    def rdb_face_acvs(self, face_id):
        del_list = list(self.redis12.smembers(face_id))
        return del_list

    # --------------db15
    def rdb_get_face(self, face_image_id):
        # 获取 id 的信息
        face = self.redis15.hgetall(face_image_id)  # 获取hashd对应的所有键值
        return face

    # def insert(name, key, value):
    #     # 插入hset
    #     return redis15.hset(name, key, value)


if __name__ == "__main__":
    ...
    # redis_client = RedisArchive()
    # print(redis_client.redis15.hgetall(None))
